package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.ShopAddress;
import com.woniuxy.common.entity.ShopUserAddress;
import com.woniuxy.common.entity.ShopUserAddressExample;
import com.woniuxy.common.mapper.ShopAddressMapper;
import com.woniuxy.common.mapper.ShopUserAddressMapper;
import com.woniuxy.joylifeshop.dto.ObjectDto;
import com.woniuxy.joylifeshop.exception.AddressException;
import com.woniuxy.joylifeshop.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    ShopUserAddressMapper shopUserAddressMapper;

    @Autowired
    ShopAddressMapper shopAddressMapper;

    /**
    * @Author SooYoo
    * @Description 找到用户的所有的映射地址对象
    * @Param
    * @Return
    **/
    @Override
    public List<ShopUserAddress> readUserMapObject(String userId) {

        try {
            //找到地址映射对象
            ShopUserAddressExample shopUserAddressExample = new ShopUserAddressExample();
            ShopUserAddressExample.Criteria criteria = shopUserAddressExample.createCriteria();
            criteria.andUserIdEqualTo(userId);

            List<ShopUserAddress> shopUserAddresses = shopUserAddressMapper.selectByExample(shopUserAddressExample);
            //通过映射对象找到地址对象
            return shopUserAddresses;
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressException();
        }

    }
    /**
    * @Author SooYoo
    * @Description 找到用户所有的实际对象
    * @Param
    * @Return
    **/
    @Override
    public List<ShopAddress> readUserRealityAddressObject(String userId) {
        try {
            //找到地址映射对象
            ShopUserAddressExample shopUserAddressExample = new ShopUserAddressExample();
            ShopUserAddressExample.Criteria criteria = shopUserAddressExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            List<ShopUserAddress> shopUserAddresses = shopUserAddressMapper.selectByExample(shopUserAddressExample);
            //通过映射对象找到地址对象
            List<ShopAddress> shopAddresses = new ArrayList<>();
            for (ShopUserAddress shopUserAddress : shopUserAddresses){

                ShopAddress shopAddress = shopAddressMapper.selectByPrimaryKey(shopUserAddress.getAddressId());
                shopAddresses.add(shopAddress);
            }
            return shopAddresses;
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressException();
        }
    }

    /**
    * @Author SooYoo
    * @Description 找到默认的地址映射对象和地址对象
    * @Param
    * @Return
    **/
    @Override
    public ObjectDto readDefaultAddress(String userId) {

        ObjectDto objectDto = new ObjectDto();

        try {

            //找到地址映射对象
            ShopUserAddressExample shopUserAddressExample = new ShopUserAddressExample();
            ShopUserAddressExample.Criteria criteria = shopUserAddressExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andPriorityEqualTo("默认");
            List<ShopUserAddress> shopUserAddresses = shopUserAddressMapper.selectByExample(shopUserAddressExample);
            //找到地址实际对象
            ShopAddress shopAddress = shopAddressMapper.selectByPrimaryKey(shopUserAddresses.get(0).getAddressId());

            objectDto.setData(shopUserAddresses);
            objectDto.setData2(shopAddress);

            return objectDto;

        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressException();
        }

    }

    /**
    * @Author SooYoo
    * @Description 找到非默认的地址映射和地址实体
    * @Param
    * @Return
    **/
    @Override
    public ObjectDto readOtherMapAddressAndAddressObject(String addrId, String userId) {

        ObjectDto objectDto = new ObjectDto();

        try {
            //找到other的映射实体
            ShopUserAddressExample shopUserAddressExample = new ShopUserAddressExample();
            ShopUserAddressExample.Criteria criteria = shopUserAddressExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andAddressIdEqualTo(addrId);
            List<ShopUserAddress> shopUserAddresses = shopUserAddressMapper.selectByExample(shopUserAddressExample);
            //通过映射实体找到地址实体
            ShopAddress shopAddress = shopAddressMapper.selectByPrimaryKey(shopUserAddresses.get(0).getAddressId());
            objectDto.setData(shopUserAddresses);
            objectDto.setData2(shopAddress);
            return objectDto;
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressException();
        }
    }

    @Override
    public ShopUserAddress readOneUserAddress(String addrId) {

        ShopUserAddressExample shopUserAddressExample = new ShopUserAddressExample();
        ShopUserAddressExample.Criteria criteria = shopUserAddressExample.createCriteria();
        criteria.andAddressIdEqualTo(addrId);


        List<ShopUserAddress> shopUserAddresses = shopUserAddressMapper.selectByExample(shopUserAddressExample);

        return shopUserAddresses.get(0);
    }
    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public ShopAddress readOneAddress(String addrId) {

        ShopAddress shopAddress = shopAddressMapper.selectByPrimaryKey(addrId);

        return shopAddress;
    }
    /**
    * @Author SooYoo
    * @Description 修改用户地址
    * @Param
    * @Return
    **/
    @Override
    public void modifyAddress(String addrId, String name, String phone,
     String country, String province, String city, String detail, boolean priority,String userId) {

        try {
            //修改useraddress
            ShopUserAddressExample shopUserAddressExample = new ShopUserAddressExample();
            ShopUserAddressExample.Criteria criteria = shopUserAddressExample.createCriteria();
            criteria.andAddressIdEqualTo(addrId);
            List<ShopUserAddress> shopUserAddresses = shopUserAddressMapper.selectByExample(shopUserAddressExample);
            ShopUserAddress shopUserAddress = shopUserAddresses.get(0);

            shopUserAddress.setAddressId(addrId);
            if (priority==false){
                shopUserAddress.setPriority("非默认");
            }else {
                shopUserAddress.setPriority("默认");
                //这个用户的其他东西就不能默认了
                ShopUserAddressExample shopUserAddressExample2 = new ShopUserAddressExample();
                ShopUserAddressExample.Criteria criteria2 = shopUserAddressExample2.createCriteria();
                criteria2.andUserIdEqualTo(userId);
                List<ShopUserAddress> shopUserAddressesByUserIds = shopUserAddressMapper.selectByExample(shopUserAddressExample2);

                for(ShopUserAddress shopUserAddress1 :shopUserAddressesByUserIds){

                    shopUserAddress1.setPriority("非默认");
                    shopUserAddressMapper.updateByPrimaryKey(shopUserAddress1);

                }

            }
            shopUserAddress.setPhone(phone);
            shopUserAddress.setName(name);

            shopUserAddressMapper.updateByPrimaryKey(shopUserAddress);

            //修改address
            ShopAddress shopAddress = shopAddressMapper.selectByPrimaryKey(addrId);

            shopAddress.setProvince(province);
            shopAddress.setCity(city);
            shopAddress.setCountry(country);
            shopAddress.setDetail(detail);

            shopAddressMapper.updateByPrimaryKey(shopAddress);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressException();
        }


    }


}
